package uk.co.hs.dao.web.admin;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import uk.co.hs.dao.generic.UtilisoftGenericDao;
import uk.co.hs.domain.web.WebRole;
import uk.co.hs.domain.web.WebRoleUrl;
import uk.co.hs.domain.web.WebUrl;

@Repository("hs_webRoleUrlDao")
public class WebRoleUrlDao extends UtilisoftGenericDao<WebRoleUrl, Long>
{
  /**
   * Get Web Role Urls by Role.
   * @param aUsername The Web Role.
   * @return The Web Role Urls.
   */
  @SuppressWarnings("unchecked")
  public List<WebRoleUrl> getWebRoleUrls(final WebRole aWebRole)
  {
    Query query = getSession().createQuery("FROM WebRoleUrl roleUrl WHERE roleUrl.role = :role");
    query.setParameter("role", aWebRole);
    return query.list();
  }

  /**
   * Get Web Role Urls by Role ID.
   * @param aUsername The Web Role ID.
   * @return The Web Role Urls.
   */
  @SuppressWarnings("unchecked")
  public List<WebRoleUrl> getWebRoleUrls(final Long aWebRolePk)
  {
    Query query = getSession().createQuery("FROM WebRoleUrl roleUrl WHERE roleUrl.role.pk = :pk");
    query.setParameter("pk", aWebRolePk);
    return query.list();
  }

  /**
  * GetRoleUrl.
  *
  * @param aUrl
  * @return RoleUrl
  */
  public WebRoleUrl getRoleUrl(final WebUrl aUrl, final WebRole aRole)
  {
    Query query = getSession().createQuery("FROM WebRoleUrl roleUrl WHERE roleUrl.role = :role AND roleUrl.url = :url");
    query.setParameter("role", aRole);
    query.setParameter("url", aUrl);
    return (WebRoleUrl) query.uniqueResult();
  }
}
